package com.dhcc.android.base.common.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.dhcc.android.base.R;
import com.dhcc.android.base.common.bean.SortModel;

import java.util.ArrayList;
import java.util.List;

public class SortAdapter extends BaseAdapter implements SectionIndexer {
    private List<SortModel> list = null;
    private Activity activity;
    private Boolean isSingle;
    private String selectedPosition = "";// 选中的位置
    private ArrayList<String> selectedPosition_multi;// 多选
    private ImageView selectImage;
    private String beanType;

    public SortAdapter(Activity activity, List<SortModel> list,
                       Boolean isSingle, String beanType) {
        this.activity = activity;
        this.list = list;
        this.isSingle = isSingle;
        this.beanType = beanType;
    }

    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     *
     * @param list
     */
    public void updateListView(List<SortModel> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void clear() {
        this.list.clear();
    }

    public int getCount() {
        return this.list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public void setSelectedPosition(ArrayList<String> str) {
        selectedPosition_multi = str;
    }

    public void setSelectedPosition(String str) {
        selectedPosition = str;
    }

    public View getView(final int position, View view, ViewGroup arg2) {
        ViewHolder viewHolder = null;
        final SortModel mContent = list.get(position);
        if (view == null) {
            viewHolder = new ViewHolder();
//			if (beanType.contains("keyValueBean")) {
            view = LayoutInflater.from(activity).inflate(
                    R.layout.util_search_item, null);
            viewHolder.tvTitle = (TextView) view.findViewById(R.id.title);
            viewHolder.select_item = (ImageView) view
                    .findViewById(R.id.select_item);
//			}
        /*else if (beanType.contains("userBean")) {
				view = LayoutInflater.from(activity).inflate(
						R.layout.notice_contact_list_item, null);
				viewHolder.photo = (RoundImageView) view
						.findViewById(R.id.contact_photo);
				viewHolder.name = (TextView) view
						.findViewById(R.id.contact_name);
				viewHolder.department = (TextView) view
						.findViewById(R.id.department);
			}*/
            viewHolder.tvLetter = (TextView) view.findViewById(R.id.catalog);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        // 根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(position);

        // 如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if (position == getPositionForSection(section)) {
            viewHolder.tvLetter.setVisibility(View.VISIBLE);
            viewHolder.tvLetter.setText(mContent.getSortLetters());
        } else {
            viewHolder.tvLetter.setVisibility(View.GONE);
        }
//		if (beanType.contains("keyValueBean")) {
        selectImage = viewHolder.select_item;
        selectImage.setVisibility(View.GONE);
        String name = this.list.get(position).getName();
        String key = this.list.get(position).getId();
        viewHolder.tvTitle.setText(name);
        if (!isSingle) {// 多选
            if (selectedPosition_multi != null) {
                for (String str : selectedPosition_multi) {
                    setImage(str, key);
                }
            }
        } else {
            setImage(selectedPosition, key);
        }
//		} else if (beanType.contains("userBean")) {
//			viewHolder.department.setText(this.list.get(position)
//					.getDepartment());
//			viewHolder.name.setText(this.list.get(position).getName());
//			// viewHolder.photo.setBackgroundResource(resid)//照片
//		}

        return view;

    }

    public void setImage(String selectedPosition, String key) {
        if (selectedPosition != null) {
            if (selectedPosition.equals(key)) {
                selectImage.setVisibility(View.VISIBLE);
            }
        }
    }

    final static class ViewHolder {
        TextView tvLetter;
        TextView tvTitle;
        ImageView select_item;
        TextView department;
        TextView name;
//		RoundImageView photo;
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */
    public int getSectionForPosition(int position) {
        if (list != null) {
            return list.get(position).getSortLetters().charAt(0);
        }
        return 0;
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = list.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }

    /**
     * 提取英文的首字母，非英文字母用#代替。
     *
     * @param str
     * @return
     */
    private String getAlpha(String str) {
        String sortStr = str.trim().substring(0, 1).toUpperCase();
        // 正则表达式，判断首字母是否是英文字母
        if (sortStr.matches("[A-Z]")) {
            return sortStr;
        } else {
            return "#";
        }
    }

    @Override
    public Object[] getSections() {
        return null;
    }
}